// pages/index/user.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    istrue: false,
    moneyOutShow:false,
    formData: {

    },
    rules: [{
      name: 'payMoney',
      rules: {
        required: true,
        message: '输入充值金额'
      }

    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setStorageSync('openid', getApp().globalData.openid);
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      method: "GET",
      url: getApp().data.servser + '/wx/mini/user/detail',
      data: {
        openId: getApp().globalData.openid
      },
      success: function(res) {
        that.setData({
          wxMiniUser: res.data.data.wxMiniUser,
          sum:res.data.data.sum
        })
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  addPark: function() {
    wx.navigateTo({
      url: '/pages/myPark/myPark',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
   
  },
  parkinginfo: function() {
    wx.navigateTo({
      url: 'parkingplacelist',
    })
  },
  outMoney: function () {
    this.setData({
      moneyOutShow: true
    })
  },
  addMoney: function() {
    this.setData({
      istrue: true
    })
  },
  out: function () {
    var that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      method: "POST",
      url: getApp().data.servser + '/wx/mini/user/out',
      data: {
        openId: getApp().globalData.openid,
        money: this.data.formData.outMoney
      },
      success: function (res) {
        that.setData({
          moneyOutShow: false,
          wxMiniUser: res.data.data.wxMiniUser
        })
      }
    })
  },
  pay: function() {
    console.log(this.data.formData.payMoney)
    var that = this;
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      method: "POST",
      url: getApp().data.servser + '/wx/mini/user/pay',
      data: {
        openId: getApp().globalData.openid,
        money: this.data.formData.payMoney
      },
      success: function (res) {
        that.setData({
          istrue:false,
          wxMiniUser: res.data.data.wxMiniUser
        })
      }
    })
  },
  selectMoney(e){
    console.log(e.currentTarget.dataset.money)
    this.setData({
      [`formData.payMoney`]: e.currentTarget.dataset.money
    })
  },
  formInputChange(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  unbound_click: function() {
    this.setData({
      istrue: true
    })
  },
  closeDialog: function() {
    this.setData({
      istrue: false
    })
  },
  change_pwd_click: function() {
    wx.navigateTo({
      url: '/pages/index/pwd'
    })
  },
  report_history_click: function() {
    wx.navigateTo({
      url: '/pages/index/report_history'
    })
  },
  unboundDialog: function() {
    wx.clearStorageSync();
    this.setData({
      istrue: false
    })
    // 登录失败
    let url = "/pages/index/index";
    wx.reLaunch({
      url: url,
      success(res) {
        let page = getCurrentPages().pop();
        if (page == undefined || page == null) {
          return
        }
        page.onShow();
      }
    })
  }
})